package com.boot.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.boot.entity.Concern;
import com.boot.entity.Store;
import com.boot.mapper.ConcernDao;
import com.boot.mapper.StoreDao;
import com.boot.service.ConcernService;
import com.boot.util.AdResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ConcernServiceImpl extends ServiceImpl<ConcernDao, Concern> implements ConcernService {

	@Resource
	private ConcernDao concernDao;
	@Resource
	private StoreDao storeDao;
	
	@Override
	@Transactional
	public AdResult concernStoreService(Concern concern) {
		AdResult adResult=new AdResult();
		//判断是否已经关注
		Concern concernGeted=concernDao.getConcern(concern);
		if(concernGeted!=null){
			int concern_result=concernDao.removeConcern(concern);
			storeDao.reduceConcernNumber(concern.getStore_id());
			if(concern_result>0){
				adResult.setMsg("取消关注");
				adResult.setStatus(2);
			}else{
				adResult.setMsg("操作失败");
				adResult.setStatus(0);
			}
		}else{
			Date date=new Date();
			concern.setTime(date);
			Store store=storeDao.getStore(concern.getStore_id());
			if(store!=null){
				concern.setStore_name(store.getName());
			}
			int insert_line =concernDao.concernStoreDao(concern);
			storeDao.increateConcernNumber(concern.getStore_id());
			if(insert_line>0){
				adResult.setMsg("关注成功");
				adResult.setStatus(1);
			}else{
				adResult.setMsg("关注失败");
				adResult.setStatus(0);
			}
		}
		return adResult;
	}

	@Override
	public AdResult isConsern(Concern concern) {
		AdResult adResult=new AdResult();
		Concern gotconcern =concernDao.getConcern(concern);
		if(gotconcern!=null){
			adResult.setMsg("已经关注");
			adResult.setStatus(1);
			//改变未读状态为已读
			concernDao.changeReadedToOne(concern);
		}else{
			adResult.setMsg("未关注");
			adResult.setStatus(0);
		}
		return adResult;
	}

	@Override
	public AdResult removeConsern(Concern concern) {
		AdResult adResult=new AdResult();
		int result_line=concernDao.removeConcern(concern);
		storeDao.reduceConcernNumber(concern.getStore_id());
		if(result_line>0){
			adResult.setMsg("成功取消");
			adResult.setStatus(1);
		}else{
			adResult.setMsg("取消失败");
			adResult.setStatus(0);
		}
		return adResult;
	}

}
